package com.baboba.rummy.game;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.baboba.rummy.player.HumanPlayer;
import com.baboba.rummy.player.Player;
import com.baboba.rummy.player.RobotPlayer;

public class GameManager {
	
	private static GameManager instance = new GameManager();
	
	private Date gameStartTime;
	private Date roundStartTime;
	private Player[] players;
	private List<GameEventHandler> eventHandlers;
	private GameRound currentRound;
	
	public static GameManager getInstance(){
		return instance;
	}
	
	public GameManager(){
		eventHandlers = new LinkedList<GameEventHandler>();
	}
	
	public void createNewGame(){
		gameStartTime = new Date();
		
		players = new Player[2];
		players[0] = new RobotPlayer("Robot", "Baboba");
		players[1] = new HumanPlayer("Me", "Yao Yuan");
		
		notifyCreateNewGameEvent();
	}
	
	public void createNewRound(){
		notifyCreateNewRoundEvent();
		currentRound = new GameRound();
		currentRound.begin();
	}
	
	public void addEventHandler(GameEventHandler handler){
		if (handler!=null && !eventHandlers.contains(handler))
			eventHandlers.add(handler);
	}

	public Date getRoundStartTime() {
		return roundStartTime;
	}

	public Date getGameStartTime() {
		return gameStartTime;
	}

	public Player[] getPlayers() {
		return players;
	}
	
	private void notifyCreateNewGameEvent(){
		for (GameEventHandler handler : eventHandlers)
			handler.onCreateNewGame();
	}
	
	private void notifyCreateNewRoundEvent(){
		for (GameEventHandler handler : eventHandlers)
			handler.onCreateNewRound();
	}
}
